package com.zzs.restweb.controller;


import com.zzs.restweb.entity.User;
import com.zzs.restweb.service.IUserService;
import com.zzs.restweb.utils.enums.SexTypeEnum;
import com.zzs.restweb.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzs
 * @since 2020-06-23
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private IUserService userService = null;

    @GetMapping("user")
    @ResponseStatus(HttpStatus.OK)
    public Object user(){
        User user = userService.getById(1);
        log.info("user.getId():{}",user.getId());
        log.info("user:{}",user);
        log.info("changToVo:userVo:{}",changToVo(user));
        return changToVo(user);
    }

    private UserVo changToVo(User user){
        UserVo userVo = new UserVo();
        userVo.setId(user.getId());
        userVo.setUserName(user.getUserName());
        userVo.setSexCode(user.getSex());
        userVo.setSexName(SexTypeEnum.getEnumByCode(user.getSex()).getDescription());
        userVo.setNode(user.getNode());
        return userVo;
    }

    private List<UserVo> changToVo(List<User> user){
        return user.stream().map(this::changToVo).collect(Collectors.toList());
    }
}
